package org.vectrics.recruiter;

import java.util.Iterator;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.vectrics.common.util.DateSync;
import org.vectrics.company.CompanyPersonGroupType;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.Filter;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.job.JobCandidateStatusReasonXref;
import org.vectrics.recruiter.job.JobStatusReasonTypeXref;
import org.vectrics.recruiter.job.JobStatusType;
import org.vectrics.recruiter.job.JobStatusTypeApproval;
import org.vectrics.recruiter.job.JobStatusTypeApprovalSet;
import org.vectrics.system.ApplicationDomainCopyUtil;
import org.vectrics.system.SystemConstant;

public class RecruiterDomainCopyUtil implements ApplicationDomainCopyUtil {
	private transient Logger log = Logger.getLogger(this.getClass());
	
	public RecruiterDomainCopyUtil() {
		super();
	}

	public void execute(Domain fromDomain, Domain toDomain, Properties properties) {
		log.debug("Clear system constants that shouldn't be copied");
		setSystemConstant(toDomain, "resume.recipient.email.addr", "");
		setSystemConstant(toDomain, "resume.recipient.email.password", "");
		setSystemConstant(toDomain, "resume.recipient.email.host", "");
		
		log.debug("Copy job status reason types");
		Filter filter = new Filter();
		filter.addFrom("JobStatusReasonTypeXref", "xref");
		filter.addSelectField("distinct xref");
		filter.addParameterCondition("xref.domain.id", "=", fromDomain.getId());
		Query query = filter.createQuery(ThreadProperties.getSession());
		Iterator iter = query.iterate();
		while (iter.hasNext()) {
			JobStatusReasonTypeXref xref = (JobStatusReasonTypeXref)iter.next();
			JobStatusReasonTypeXref newXref = new JobStatusReasonTypeXref();
			newXref.setId(CoreServiceLocator.getSurrogateKeyService().getKey("job.sts.rsn.xref.id"));
			newXref.setDomain(toDomain);
			newXref.setDefault(xref.getDefault());
			newXref.setFromStatusType(xref.getFromStatusType());
			newXref.setToStatusType(xref.getToStatusType());
			newXref.setStatusReasonType(xref.getStatusReasonType());
			newXref.setDefault(xref.getDefault());
			newXref.setVoided(xref.getVoided());
			ThreadProperties.getSession().save(newXref);
		}
		
		JobStatusTypeApprovalSet approvals = RecruitServiceLocator.getJobProvider().findJobStatusApprovals(fromDomain);
		iter = approvals.iterator();
		while (iter.hasNext()) {
			JobStatusTypeApproval approval = (JobStatusTypeApproval)iter.next();
			addStatusTypeApprovalGroup(toDomain, approval.getFromJobStatusType(), approval.getToJobStatusType(), 
					approval.getCompanyPersonGroupType());
		}
		
		copyJobCandidateStatusConfig(fromDomain, toDomain);
		//HtmlFormType formType = CoreServiceLocator.getSystemService().findHtmlFormType("public.standard"); 
		//ThemeBuilder themeBuilder = new RedAndBlackThemeBuilder(toDomain, formType);
		//themeBuilder.execute();
	}

	
	private void copyJobCandidateStatusConfig(Domain fromDomain, Domain toDomain) {
		Filter filter = new Filter();
		filter.addFrom("JobCandidateStatusReasonXref", "xref");
		filter.addSelectField("distinct xref");
		filter.addParameterCondition("xref.domain.id", "=", fromDomain.getId());
		Query query = filter.createQuery(ThreadProperties.getSession());
		Iterator iter = query.iterate();
		while (iter.hasNext()) {
			JobCandidateStatusReasonXref xref = (JobCandidateStatusReasonXref)iter.next();
			JobCandidateStatusReasonXref newXref = new JobCandidateStatusReasonXref();
			newXref.setId(CoreServiceLocator.getSurrogateKeyService().getKey("job.cand.sts.rsn.xref.id"));
			newXref.setDomain(toDomain);
			newXref.setDefault(xref.getDefault());
			newXref.setFromStatusType(xref.getFromStatusType());
			newXref.setToStatusType(xref.getToStatusType());
			newXref.setStatusReasonType(xref.getStatusReasonType());
			newXref.setDefault(xref.getDefault());
			newXref.setVoided(xref.getVoided());
			ThreadProperties.getSession().save(newXref);
		}
	}
	
	private void setSystemConstant(Domain domain, String code, String newValue) {
		SystemConstant systemConstant = CoreServiceLocator.getSystemService().findSystemConstant(domain, code);
		systemConstant.setValue(newValue);
		CoreServiceLocator.getSystemService().updateSystemConstant(systemConstant);
	}
	

	private void addStatusTypeApprovalGroup(Domain domain, JobStatusType fromStatusType, 
					JobStatusType toStatusType, CompanyPersonGroupType groupType) {
		
		Long nextId = RecruitServiceLocator.getSurrogateKeyService().getKey("job.sts.appr");
		
		JobStatusTypeApproval approval = new JobStatusTypeApproval();
		approval.setId(nextId);
		approval.setCompanyPersonGroupType(groupType);
		approval.setDomain(domain);
		approval.setStartDate(DateSync.getCurrentTime());
		approval.setFromJobStatusType(fromStatusType);
		approval.setToJobStatusType(toStatusType);
		ThreadProperties.getSession().save(approval);
	}

}
